<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\eslib\EsUtils;
use app\model\admin\EsxdmAuthAdminModel;
use app\model\admin\EsxdmAuthGroupModel;
use think\facade\Validate;
use think\helper\Str;
use think\Request;

class EsxdmAuthAdmin
{
    protected $middleware = [\app\middleware\JwtAuthForAdmin::class];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $groupName = $request->param('group_name', '');
        $searchText = $request->param('search_text', '');
        $pageSize = intval($request->param('page_size', 10));

        $data = EsxdmAuthAdminModel::dbView()->where(function ($query) use ($groupName, $searchText) {
            if ($groupName > 0) {
                $query->where('group_name', $groupName);
            }
            if ($searchText) {
                $query->where(function ($query) use ($searchText) {
                    $query->where('id', intval($searchText))->whereOr('user_name', 'like', "%{$searchText}%");
                });
            }
        })->order('status')->order('id', 'desc')->paginate($pageSize);

        return json(EsUtils::stdmessage(1, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->param();
        $validator =  Validate::rule([
            'user_name' => 'require|max:150',
            'nick_name' => 'require|max:150',
            'password' => 'require|max:150',
            'group_name' => 'require|max:150',
            'avatar' => 'require|max:150',
            'email' => 'require|email',
            'mobile' => 'require|number',
            'status' => 'require|number',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdmessage(0, $validator->getError()));
        }
        // 表单数据处理
        $formData['register_ip'] = $request->ip();
        $formData['salt'] = Str::random(8);
        $formData['password'] = EsUtils::hashPassword($formData['password'], $formData['salt']);
        try {
            $model = EsxdmAuthAdminModel::create($formData);
            return json(EsUtils::stdmessage(1, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdmessage(0, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = EsxdmAuthAdminModel::find($id);
            if (!$model) {
                return json(EsUtils::stdmessage(0, '记录不存在'));
            }
        } else {
            $model = new EsxdmAuthAdminModel();
            $model->status = 1;
        }
        $data = [
            'data' => $model,
            'status_data' => $model->getStatusDataAttr(),
            'gender_data' => $model->getGenderDataAttr(),
            'group_name_data' => EsxdmAuthGroupModel::getGroupData(),
        ];
        return json(EsUtils::stdmessage(1, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->param();
        $validator =  \think\facade\Validate::rule([
            'user_name' => 'require|max:150',
            'nick_name' => 'require|max:150',
            'password' => 'require|max:150',
            'group_name' => 'require|max:150',
            'avatar' => 'require|max:150',
            'email' => 'require|email',
            'mobile' => 'require|number',
            'status' => 'require|number',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdmessage(0, $validator->getError()));
        }
        try {
            $affected = EsxdmAuthAdminModel::where('id', $id)->update($formData);
            return json(EsUtils::stdmessage(1, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = EsxdmAuthAdminModel::destroy($id);
            return json(EsUtils::stdmessage(1, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }

    /**
     * 复制指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function copy($id)
    {
        try {
            $affected = EsxdmAuthAdminModel::copy($id);
            return json(EsUtils::stdmessage(1, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }
}
